<template>
  <div>
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo">
        <el-form-item label="操作人">
          <el-input v-model="searchInfo.userName" placeholder="搜索条件" clearable/>
        </el-form-item>
		<el-form-item label="名称">
          <el-input v-model="searchInfo.name" placeholder="支持模糊搜索" clearable/>
        </el-form-item>
		<el-form-item label="请求路径">
		  <el-input v-model="searchInfo.path" placeholder="支持模糊搜索" clearable/>
		</el-form-item>
		<el-form-item label="IP">
		  <el-input v-model="searchInfo.ip" placeholder="搜索条件" clearable/>
		</el-form-item>
		<el-form-item label="请求方法">
          <el-input v-model="searchInfo.method" placeholder="搜索条件" clearable/>
        </el-form-item>
        <el-form-item label="结果状态码">
          <el-input v-model="searchInfo.code" placeholder="搜索条件" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <el-table
        :data="tableData"
        style="width: 100%"
        tooltip-effect="dark"
        row-key="id"
      >
        <el-table-column align="left" label="操作人" prop="userName" width="140" />
        <el-table-column align="left" label="日期" width="160">
          <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column align="left" label="状态码" prop="code" width="80">
          <template #default="scope">
            <div>
              <el-tag type="success">{{ scope.row.code }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="返回信息" prop="message" width="140" />
        <el-table-column align="left" label="名称" prop="name" width="140" />
        <el-table-column align="left" label="请求IP" prop="ip" width="100" />
        <el-table-column align="left" label="请求方法" prop="method" width="80" />
        <el-table-column align="left" label="请求路径" prop="path" width="240" />
        <el-table-column align="left" label="执行时间(ms)" prop="latency" width="110" />
        <el-table-column align="left" label="请求" prop="param" width="60">
          <template #default="scope">
            <div>
              <el-popover v-if="scope.row.param" placement="left-start" trigger="click">
                <div class="popover-box">
                  <pre>{{ fmtBody(scope.row.param) }}</pre>
                </div>
                <template #reference>
                  <el-icon style="cursor: pointer;"><warning /></el-icon>
                </template>
              </el-popover>
              <span v-else>无</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="响应" prop="result" width="60">
          <template #default="scope">
            <div>
              <el-popover v-if="scope.row.result" placement="left-start" trigger="click">
                <div class="popover-box">
                  <pre>{{ fmtBody(scope.row.result) }}</pre>
                </div>
                <template #reference>
                  <el-icon style="cursor: pointer;"><warning /></el-icon>
                </template>
              </el-popover>
              <span v-else>无</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { getOperateLogList } from '@/api/operateLog' // 此处请自行替换地址
import { formatDate } from '@/utils/format'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const pageNumber = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})
const onReset = () => {
  searchInfo.value = {}
}
// 条件搜索前端看此方法
const onSubmit = () => {
  pageNumber.value = 1
  pageSize.value = 10
  if (searchInfo.value.status === '') {
    searchInfo.value.status = null
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  pageNumber.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getOperateLogList({
    pageNumber: pageNumber.value,
    pageSize: pageSize.value,
    ...searchInfo.value,
  })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
  }
}

getTableData()

const fmtBody = (value) => {
  try {
    return JSON.parse(value)
  } catch (err) {
    return value
  }
}
</script>

<script>
export default {
  name: 'SysOperationRecord'
}
</script>

<style lang="scss">
.table-expand {
  padding-left: 60px;
  font-size: 0;
  label {
    width: 90px;
    color: #99a9bf;
    .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 50%;
    }
  }
}
.popover-box {
  background: #112435;
  color: #f08047;
  height: 600px;
  width: 420px;
  overflow: auto;
}
.popover-box::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
</style>
